<template>
	<view>
		<u-sticky bgColor="#f7f7f7">
			<view class="head-inp">
				<input v-model="content" placeholder="查询当前店铺商品" placeholder-style="color: #999;" />
				<text v-show="content" @click="searchPro(0)" class="search-btn">查询</text>
			</view>
		</u-sticky>
		<view class="pro-list" v-show="show">
			<u-empty v-if="proList == ''" mode="data" style="padding-top: 100rpx;" iconColor="#f18b49" text="店铺商品上架中,再等等"></u-empty>
			<view class="pro-list-box" v-else>
				<view class="list-box" v-for="(item,index) in proList" @click="toProductDetail(item.id)">
					<view class="list-img">
						<image :src="baseUrl + item.img" mode="widthFix"></image>
					</view>
					<view class="list-cont">{{ item.name }}</view>
					<view class="price-box">
						<view style="font-size: 28rpx;color: #999;">
							<text>{{ item.true_name }}</text>
						</view>
						<view class="list-price">
							<text>￥</text>{{ item.sell_price }}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				page: 1,
				content: '',
				proList: [],
				show: false,
				baseUrl: 'http://www.hy-dzsc.com/'
			}
		},
		onLoad(e) {
			this.id = e.id
			this.getProList()
		},
		onReachBottom(){
			this.page++
			if(this.content){
				this.searchPro(1)
			} else {
				this.getProList()
			}
		},
		methods: {
			// 获取商品列表
			getProList(){
				uni.request({
					url: `${this.baseUrl}gjcApi/getCaGoodsList?id=${this.id}&page=${this.page}`,
					data: {},
					header: { 'content-type': 'application/x-www-form-urlencoded' },
					method:'GET',
					success: res => {
						this.listPack(res.data.goodsList)
					}
				})
			},
			// 搜索当前商品
			searchPro(num){
				if(num == 0){
					this.page = 1
					this.proList = []
					this.reqPack()
				} else {
					this.reqPack()
				}
			},
			// 请求封装
			reqPack(){
				uni.request({
					url: `${this.baseUrl}gjcApi/fuzzyQuery?id=${this.id}&page=${this.page}&name=${this.content}`,
					data: {},
					header: { 'content-type': 'application/x-www-form-urlencoded' },
					method:'GET',
					success: res => {
						this.listPack(res.data)
					}
				})
			},
			// 列表渲染封装
			listPack(res) {
				if(res.length == 0 && this.page > 1){
					this.page--
					uni.showToast({
						title: '没有更多数据了',
						duration: 1000,
						icon: 'none'
					})
				} else {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					setTimeout(() => {
						uni.hideLoading()
						this.show = true
						res.forEach(v => {
							this.proList.push(v)
						})
					},800)
				}
			},
			// 跳转商品详情
			toProductDetail(id){
				uni.navigateTo({
					url: `/pages/enquiry/enquiry-detail?id=${id}`
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
.head-inp{
	display: flex;
	padding: 15rpx;
	align-items: center;
	input{
		width: 750rpx;
		font-size: 30rpx;
		position: relative;
		border-radius: 40rpx;
		padding: 19rpx 158rpx 19rpx 20rpx;
		border: 2rpx solid $uni-text-color;
	}
	.search-btn{
		right: 21rpx;
		font-size: 28rpx;
		position: absolute;
		background: #ff914d;
		padding: 19rpx 50rpx;
		border-radius: 40rpx;
		color: $uni-model-color;
	}
}
.pro-list {
	padding: 0 10rpx;
	.pro-list-box {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		.list-box {
			width: 360rpx;
			border-radius: 15rpx;
			margin-bottom: 15rpx;
			padding-bottom: 20rpx;
			background: $uni-model-color;
			.list-img {
				text-align: center;
				padding: 10rpx 10rpx 0 10rpx;
				image {
					width: 100%;
					border-radius: 10rpx;
				}
			}
			.list-cont {
				font-size: 28rpx;
				overflow: hidden;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				margin: 6rpx 0 30rpx 0;
				padding: 5rpx 20rpx 0 20rpx;
				-webkit-box-orient: vertical;
				text {
					color: $uni-model-color;
					font-size: 24rpx;
					margin-right: 6rpx;
					padding: 2rpx 12rpx;
					border-radius: 20rpx;
					background: $uni-text-color;
				}
			}
			.price-box {
				display: flex;
				padding: 0 20rpx;
				align-items: center;
				justify-content: space-between;
				.list-price {
					font-size: 36rpx;
					color: $uni-text-color;
					text {
						font-size: 24rpx;
					}
				}
			}
		}
	}
}
</style>